public class test3 {
        //力扣  1683.将x减到0的最小操作数
    public static void main(String[] args) {
        int[] nums = {5, 6, 7, 8, 9};
        int x = 4;
        System.out.println(minOperations(nums, x));


    }

    public static int minOperations(int[] nums, int x) {
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }
        int left = 0;
        int right = 0;
        int tmp = 0;
        int target = sum - x;
        if (target < 0) {
            return -1;
        }
        int ret = -1;
        for (; right < nums.length; right++) {
            tmp += nums[right];
            while (tmp > target) {
                tmp -= nums[left];
                left++;
            }
            if (tmp == target) {
                ret = Math.max(ret, right - left + 1);
            }
        }
        if (ret == -1) {
            return -1;
        }
        return nums.length - ret;
    }
}
